Skip to main content

Search components

How to fetch components

There are many ways to search for a component, here you will see all of them.​


Selecting the component through the interface.​

  • Just create a public variable of your component and select it in the properties panel.

In your Java class, do the following:​


package JAVARuntime;

public class YourClass extends Component {

// just for the example, this component can be replaced with any other component
public SUIImage myComponent; // select in properties

@Override
public void start() {

}

@Override
public void repeat() {

}

}

And then select the component in the properties panel:​


Doing a direct search of the component by script.​

  • Just create a private variable of your component and fetch it from function start.

In your Java class, do the following:​


package JAVARuntime;

public class YourClass extends Component {

// just for the example, this component can be replaced with any other component
private SUIImage myComponent;

@Override
public void start() {

// find the component
myComponent = (SUIImage) WorldController.findObject("Name of the object the component is attached to").findComponent("SUIImage");

}

@Override
public void repeat() {

}

}

Doing a direct search of the component in the object itself that the script and component are attached by script.​

  • Just create a private variable of your component and fetch it from function start.

In your Java class, do the following:​


package JAVARuntime;

public class YourClass extends Component {

// just for the example, this component can be replaced with any other component
private SUIImage myComponent;

@Override
public void start() {

// find the component
myComponent = myObject.findComponent("SUIImage");

}

@Override
public void repeat() {

}

}

Another possible way is to use a variable of type SpatialObject.​

  • Just create a public variable of type SpatialObject, select the object to which the component is attached in the properties panel and use the concepts shown in the previous examples.

In your Java class, do the following:​


package JAVARuntime;

public class YourClass extends Component {

// object to which the component is attached
public SpatialObject object; // select in properties

// just for the example, this component can be replaced with any other component
private SUIImage myComponent;

@Override
public void start() {

// find the component
myComponent = object.findComponent("SUIImage");

}

@Override
public void repeat() {

}

}

Doing the automatic search using a annotation​

⚠️ It only works if the script and the component are in the same object. ⚠️

  • Just create a private variable of your component and use the @AutoWired annotation in the component variable.

In your Java class, do the following:​


package JAVARuntime;

public class YourClass extends Component {

@AutoWired
private SUIImage myComponent; // just for the example, this component can be replaced with any other component

@Override
public void start() {

// @AutoWired does exactly that automatically
myComponent = myObject.findComponent("SUIImage");

}

@Override
public void repeat() {

}

}

Search physics components​

  • It's basically the same thing as searching for a component from the forms presented, however, to obtain it by script it changes a little.

In your Java class, do the following:​


package JAVARuntime;

public class YourClass extends Component {

private Rigidbody myComponent; // just for example this component can be replaced with any other physics component

public SpatialObject object; // object to which the component is present

@Override
public void start() {

// find the physics component of the object
myComponent = object.getPhysics().getPhysicsEntity();

}

@Override
public void repeat() {

}

}